package application;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

public class Window{
	private int		width			= 0;
	private int		height			= 0;
	private String	title			= "";
	
	/**
	 * Constructor.
	 * Create window with defined parameters.
	 */
	
	public Window(int width, int height, String title){
		this.setSize(width, height);
		this.setTitle(title);
		reset();
		resetComponents();		
	}
	
	public void clear(){
		glClear(GL_COLOR_BUFFER_BIT);
	}
	
	/**
	 * Sets size of the window. Cannot be redefined later.
	 */
	
	private void setSize(int width, int height){
		this.width	= width;
		this.height	= height;
	}

	/**
	 * Sets title of the window
	 * @param title
	 */
	
	public void setTitle(String title){
		this.title = title;
	}
	
	/**
	 * Gets dimension of window
	 * @return
	 */
	
	public int getWidth(){
		return width;
	}

	public int getHeight(){
		return height;
	}
	
	/**
	 * Returns title of the window.
	 * @return
	 */
	
	public String getTitle(){
		return title;
	}
	
	/**
	 * Reinitializes window.
	 */
	
	public void reset(){
		if (Display.isCreated()){
			Display.destroy();
		}
		
		// If title is empty, set it to current window size
		if (title.isEmpty()){
			title = String.valueOf(width) + "x" + String.valueOf(height);
		}
		
		// Check errors
		try{
			// Set resolution
			Display.setDisplayMode(new DisplayMode(width, height));

			
			// Set window title
			Display.setTitle(title);

			// Set graphic context attributes
			PixelFormat pixelFormat = new PixelFormat();					// configures bit depth etc 
			ContextAttribs contextAtrributes = new ContextAttribs(1, 0);	// configures OpenGL version
			Display.create(pixelFormat, contextAtrributes);					// create window
	
			/*
			 * OpenGL initialization
			 */
			glViewport(0, 0, width, height);		// visible area
			glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	// default background color
			glOrtho(0, width, height, 0, 1, -1);	// coordinates in pixels

			
			// Enable cull facing (render only front side of polygon)
			glEnable(GL11.GL_CULL_FACE);
			GL11.glFrontFace(GL11.GL_CCW);

			// Enable blending (using alpha channel)
        	glEnable(GL_BLEND);
        	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		}
		catch (LWJGLException errHandler){
			throw new RuntimeException("Error during LWJGL initialization.", errHandler);
		}
	}
	
	/**
	 * Initialize components such as keyboard, mouse etc.
	 * Do everything, what will be done definitely only once (no during every window resize etc).
	 */
	private void resetComponents(){
		try{
			Keyboard.create();
			Mouse.create();
		}
		catch (LWJGLException errHandler){
			throw new RuntimeException("Error during LWJGL initialization.", errHandler);
		}
	}
	
	/**
	 * For "infinity" loops. Checks, if window still exists (and program should run).
	 * 
	 */
	public boolean running(){
		return !Display.isCloseRequested();
	}
	
	/**
	 * Refresh the scene.
	 */
	public void update(){
		Display.update();
		//Display.sync(250);
	}

}